package com.util;

import java.util.HashMap;
import java.util.Map;

public class ShopCaiUtil {
	public static String getStr(String str, String name, String num) {
		if (!str.trim().equals("")) {
			String[] split = str.split("-");
			String test = "";
			for (String string : split) {
				// System.out.println(string);
				String[] split2 = string.split("_");
				for (int i = 0; i < split2.length;) {
					if (split2[0].trim().equals(name)) {
						int temp = Integer.parseInt(split2[1]);
						int tempnum = Integer.parseInt(num);
						tempnum = temp + tempnum;
						split2[1] = String.valueOf(tempnum);
						test = test + split2[0] + "_" + split2[1] + "-";
						// System.out.println(test+"1111111111");
						break;
					} else {
						test = test + split2[0] + "_" + split2[1] + "-";
						// System.out.println(test+"22222222222222");
						break;
					}
				}
			}
			System.out.println(test);
			//System.out.println("---------------------");
			return test;
		} else {
			System.out.println("+++++++++++NULL+++++++++++++");
			return null;
		}

	}
	public static Map<String, Object> getStrMap(String str,String name,String num) {
		// map里面封装俩个 值. 
		// 一个是全字符串  tempString
		//,另外一个是购物车的数量,并不是单个商品的数量 tempCarNum
		Map<String, Object> map=new HashMap<>();
		int tempCarNum=0;								

		//if (!str.trim().equals("")) {
			if (str!=null) {
			String[] split = str.split("-");
			//System.out.println("length:"+split.length);
			tempCarNum=split.length;
			String test="";
			int tt=0;
			for (String string : split) {
				//System.out.println(string);
				String[] split2 = string.split("_");
				for (int i = 0; i < split2.length;) {
					if (split2[0].trim().equals(name) ) {
						int temp=Integer.parseInt(split2[1]);
						int tempnum=Integer.parseInt(num);
						tempnum=temp+tempnum;
						split2[1]=String.valueOf(tempnum);
						test=test+split2[0]+"_"+split2[1]+"-";
						//System.out.println(test+"1111111111");
						break;
					}else {
						test = test + split2[0] + "_" + split2[1] + "-";
						tt++;
						//System.out.println(test+"22222222222222@@@@@@@@@"+tt);
						break;
					}
				}

			}
			//System.out.println(tt);
			if (tt==tempCarNum) {
				test=str+"-"+name+"_"+num+"-";
				tempCarNum++;
			}else {
				test=test;
			}
			map.put("tempString",test );
			map.put("tempCarNum",tempCarNum );
			//System.out.println(test);
			System.out.println("---------------------");
			return map;
		}else {
			String tet=name+"_"+num+"-";
			map.put("tempString",tet );
			map.put("tempCarNum",1 );
			System.out.println("++++++++++++++++++++++++");
			return map;
		}


	}

}
